package com.ok.activity.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ok.activity.domain.dto.OkActivityDto;
import com.ok.activity.domain.po.OkActivity;
import com.ok.activity.domain.po.OkUseractivity;
import com.ok.activity.service.IOkActivityService;
import com.ok.common.domain.po.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.reflections.util.Utils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 活动 前端控制器
 * </p>
 *
 * @author lhw
 * @since 2024-04-29
 */
@Api(tags = "活动相关接口")
@RestController
@RequestMapping("/activity")
@RequiredArgsConstructor
public class OkActivityController {

    private final IOkActivityService okActivityService;

    @ApiOperation(value = "添加活动")
    @PostMapping("/add")
    public Result add(@RequestBody OkActivityDto okActivityDto){
        okActivityService.add(okActivityDto);
        return Result.success("添加成功");
    }

    @ApiOperation(value = "删除活动")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        okActivityService.removeById(id);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "修改活动")
    @PutMapping("/update")
    public Result update(@RequestBody OkActivity okActivity){
        okActivityService.updateById(okActivity);
        return Result.success("修改成功");
    }

    @ApiOperation(value = "分页查询活动")
    @GetMapping("/page")
    public Result Page(@RequestParam Integer pageNo,@RequestParam Integer pageSize,
                       @RequestParam(required = false) String name){
        Page<OkActivity> page= okActivityService.pageWith(pageNo,pageSize,name);

        return Result.success(page);
    }

    @ApiOperation(value = "活动列表")
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String name){
        LambdaQueryWrapper<OkActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Utils.isEmpty(name),OkActivity::getName,name);
        List<OkActivity> list = okActivityService.list(queryWrapper);
        return Result.success(list);
    }

}
